﻿using JX.Core;
using JX.Core.Entity;
using JX.Infrastructure.Common;
using JX.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JX.Application
{
	/// <summary>
	/// 数据库表：Dictionary 的应用层服务接口实现类.
	/// </summary>
	public partial class DictionaryServiceAppDapper : ServiceAppDapper<DictionaryEntity>,IDictionaryServiceAppDapper
	{
		#region 得到实体
		/// <summary>
		/// 根据条件得到Dictionary表的实体类
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public DictionaryEntity Get(string tableName, string fieldName)
		{
			return m_repository.Get(p => p.TableName == tableName && p.FieldName == fieldName);
		}
		/// <summary>
		/// 根据条件得到Dictionary表的实体类
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public async Task<DictionaryEntity> GetAsync(string tableName, string fieldName)
		{
			return await m_repository.GetAsync(p => p.TableName == tableName && p.FieldName == fieldName);
		}
		#endregion

		#region 得到数据字典中字段定义的值的集合类
		/// <summary>
		/// 得到数据字典中字段定义的值的集合类
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public IList<DictionaryValue> GetDictionaryFieldValue(string tableName, string fieldName) 
		{
			return GetDictionaryFieldValue(Get(tableName, fieldName));
		}
		/// <summary>
		/// 得到数据字典中字段定义的值的集合类
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public async Task<IList<DictionaryValue>> GetDictionaryFieldValueAsync(string tableName, string fieldName)
		{
			return await GetDictionaryFieldValueAsync(Get(tableName, fieldName));
		}
		/// <summary>
		/// 得到数据字典中字段定义的值的集合类
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="isShowEnable">是否只显示已启用的值(true：只显示已启用的；false：全部；)</param>
		/// <returns></returns>
		public IList<DictionaryValue> GetDictionaryFieldValue(DictionaryEntity entity, bool isShowEnable = false)
		{
			IList<DictionaryValue> dictValueList = new List<DictionaryValue>();
			if (!string.IsNullOrEmpty(entity.FieldValue))
			{
				string[] strArray = entity.FieldValue.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
				for (int i = 0; i < strArray.Length; i++)
				{
					string[] strArray2 = strArray[i].Split(new char[] { '|' });
					if (strArray2.Length == 3)
					{
						DictionaryValue item = new DictionaryValue();
						item.DataTextField = strArray2[0];
						item.IsEnable = DataConverter.CBoolean(strArray2[1]);
						item.IsDefault = DataConverter.CBoolean(strArray2[2]);
						item.DataValueField = i;
						item.Title = entity.Title;
						if (isShowEnable)
						{
							if (item.IsEnable)
							{
								dictValueList.Add(item);
							}
						}
						else
						{
							dictValueList.Add(item);
						}
					}
				}
			}
			return dictValueList;
		}
		/// <summary>
		/// 得到数据字典中字段定义的值的集合类
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="isShowEnable">是否只显示已启用的值(true：只显示已启用的；false：全部；)</param>
		/// <returns></returns>
		public async Task<IList<DictionaryValue>> GetDictionaryFieldValueAsync(DictionaryEntity entity, bool isShowEnable = false)
		{
			return await Task.Run(() => GetDictionaryFieldValue(entity, isShowEnable));
		}
		#endregion

		#region 设置字段值
		/// <summary>
		/// 设置字段的值
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <param name="fieldValue"></param>
		/// <returns></returns>
		public bool SetFieldValue(string tableName, string fieldName, string fieldValue)
		{
			var entity = Get(tableName, fieldName);
			if (entity == null)
				return false;
			entity.FieldValue = fieldValue;
			return m_repository.Update(entity);
		}
		/// <summary>
		/// 设置字段的值
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="fieldName"></param>
		/// <param name="fieldValue"></param>
		/// <returns></returns>
		public async Task<bool> SetFieldValueAsync(string tableName, string fieldName, string fieldValue)
		{
			var entity = Get(tableName, fieldName);
			if (entity == null)
				return false;
			entity.FieldValue = fieldValue;
			return await m_repository.UpdateAsync(entity);
		}
		#endregion
	}
}